package test.simple;

import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.springframework.transaction.jta.JtaTransactionManager;

import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.Configuration;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jms.PoolingConnectionFactory;

public class LaunchActiveMQGlobalTransactionBitronix {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		org.apache.activemq.broker.BrokerService bs = new org.apache.activemq.broker.BrokerService();
		bs.setBrokerName("testBroker");
		bs.setPersistent(false);
		bs.setTransportConnectorURIs(new String[]{"tcp://localhost:61616"});
		bs.start();
		

		
		Configuration conf = TransactionManagerServices.getConfiguration();
		conf.setServerId("jvm-1");
		conf.setLogPart1Filename("./target/bitronix/part1.btm");
		conf.setLogPart2Filename("./target/bitronix/part2.btm");
		
		BitronixTransactionManager tm = bitronix.tm.TransactionManagerServices.getTransactionManager();
		
		JtaTransactionManager springTM = new JtaTransactionManager();
		springTM.setTransactionManager(tm);
		springTM.setUserTransaction(tm);
		
		PoolingConnectionFactory cf1 = new  PoolingConnectionFactory();
		cf1.setClassName("org.apache.activemq.ActiveMQXAConnectionFactory");
		cf1.setUniqueName("activemq1");
		cf1.setMaxPoolSize(5);
		cf1.getDriverProperties().put("brokerURL", "tcp://localhost:61616");
		cf1.init();
		
		PoolingConnectionFactory cf2 = new  PoolingConnectionFactory();
		cf2.setClassName("org.apache.activemq.ActiveMQXAConnectionFactory");
		cf2.setUniqueName("activemq2");
		cf2.setMaxPoolSize(5);
		cf2.getDriverProperties().put("brokerURL", "tcp://localhost:61616");
		cf2.init();
		
		org.apache.activemq.ActiveMQConnectionFactory cf3 = new org.apache.activemq.ActiveMQConnectionFactory();
		cf3.setBrokerURL("tcp://localhost:61616");
		
		
		org.apache.camel.component.jms.JmsConfiguration jmsComp1 = new org.apache.camel.component.jms.JmsConfiguration();
		jmsComp1.setConnectionFactory(cf1);
		jmsComp1.setTransacted(true);
		jmsComp1.setConcurrentConsumers(1);
		jmsComp1.setTransactionManager(springTM);

		
		org.apache.camel.component.jms.JmsConfiguration jmsComp2 = new org.apache.camel.component.jms.JmsConfiguration();
		jmsComp2.setConnectionFactory(cf2);
		jmsComp2.setTransacted(true);
		jmsComp2.setConcurrentConsumers(1);
		jmsComp2.setTransactionManager(springTM);

		
		org.apache.activemq.camel.component.ActiveMQComponent comp1 = new org.apache.activemq.camel.component.ActiveMQComponent();
		comp1.setConfiguration(jmsComp1);
		
		org.apache.activemq.camel.component.ActiveMQComponent comp2 = new org.apache.activemq.camel.component.ActiveMQComponent();
		comp2.setConfiguration(jmsComp2);
		
		
		CamelContext context = new DefaultCamelContext();
		
		context.addComponent("activemq-local-tx1", comp1);
		context.addComponent("activemq-local-tx2", comp2);
		
		
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				
				//from("activemq-local-tx1:queue:inqueue").to("log:test.bitronix.activemq?level=INFO&showAll=true&multiline=true");//.inOnly("activemq-local-tx2:outqueue");
				from("activemq-local-tx1:queue:inqueue").to("log:test.bitronix.activemq?level=INFO&showAll=true&multiline=true").to("activemq-local-tx2:queue:outqueue");//.inOnly("activemq-local-tx2:outqueue");
			}
		});
		
		
		context.start();
		
		
		
		

		
		Connection conn = cf3.createConnection();
		Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		 MessageProducer mp =s.createProducer(s.createQueue("inqueue"));
		 TextMessage tm2 = s.createTextMessage("Test message");
		mp.send(tm2); 
		tm2.setText("Test message 2");
		mp.send(tm2); 	


		
		Thread.sleep(50000);
		tm.shutdown();
		bs.stop();

	}

}
